
const isInYears = (startYear, endYear, currentYear) => {
  if (!startYear && !endYear) return true
  if (startYear && currentYear < startYear) return false
  if (endYear && currentYear > endYear) return false
  return true
}

export const initProjectData = (dataSource, start, end) => {
  // 计算总数量
  const { countDetails } = dataSource
  const cubeData = []
  const targetData = Object.values(countDetails).at(0).map(item => { // 获取公司列表
    return {
      name: item.name,
      count: 0
    }
  })

  const targetObject = targetData.reduce((prev, current) => {
    prev[current.name] = current
    return prev
  }, {})
  // 构建数据
  Object.entries(countDetails).forEach(([year, yearData]) => {
    // 判断year是否被包含
    const inYear = isInYears(start, end, year)

    yearData.forEach(singleItem => {
      const { count, name } = singleItem
      if (inYear) { // 左边数量+
        targetObject[name]['count'] += count
      }
      cubeData.push([
        name, year, count, inYear
      ])
    })
  })
  // 构建tree的数据结构

  return [targetData, cubeData]
}
